package com.fumin.controller;

import com.fumin.common.ApplicationContextHelper;
import com.fumin.common.JsonData;
import com.fumin.dao.SysAclMapper;
import com.fumin.exception.ParamException;
import com.fumin.exception.PermissionExcetion;
import com.fumin.model.SysAcl;
import com.fumin.param.TestVo;
import com.fumin.util.BeanValidator;
import com.fumin.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 校验工具,检测请求参数是否合法
 * @Author xiayuxuanmin
 * @Date 2019/10/5
 * @Description
 */
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {

    @ResponseBody
    @RequestMapping("/hello")
    public JsonData hello(){
        log.info("hello");
//        return "hello,myPermission";
        return JsonData.success("hello,myPermission");
    }

    @ResponseBody
    @RequestMapping("/exceptionTest.json")
    public JsonData exceptionTest(){//测试自定义异常
        log.info("exceptionTest");
        //所有返回的请求都会被SpringExceptionResolver捕获到
        throw new PermissionExcetion("test permissionException");
//        return JsonData.success( "hello,myPermission");
    }

    @ResponseBody
    @RequestMapping("/systemExceptionTest.json")
    public JsonData systemExceptionTest(){//测试系统异常
        log.info("systemExceptionTest");
        //所有返回的请求都会被SpringExceptionResolver捕获到
        throw new RuntimeException("test permissionException");
//        return "hello,myPermission";
    }

    @ResponseBody
    @RequestMapping("/validate.json")
    public JsonData validate(TestVo vo){
        log.info("validate");
        try{
            Map<String, String> map = BeanValidator.validateObject(vo);
            if (MapUtils.isNotEmpty(map)) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    log.info("{}->{}",entry.getKey(),entry.getValue());
                }
            }
        }catch (Exception e){
        }
        return JsonData.success("test validate");
    }

    /**
     * 测试请求参数是否合法
     * throws ParamException表示方法可能会抛出一个异常
     * @param vo
     * @return
     * @throws ParamException
     */
    @ResponseBody
    @RequestMapping("/validateException.json")
    public JsonData validateException(TestVo vo) throws ParamException {
        log.info("validateException");
        BeanValidator.check(vo);
        return JsonData.success("validateException");
    }

    @ResponseBody
    @RequestMapping("/getBeanAndJsonMapper.json")
    public JsonData testGetBeanAndJsonMapper(){
        log.info("testGetBeanAndJsonMapper");
        //这里测试没有spring上下文,看是否可以通过helper是否可以获取对应的bean对象
        SysAclMapper sysAclMapper = ApplicationContextHelper.popBean(SysAclMapper.class);
        SysAcl sysAcl = sysAclMapper.selectByPrimaryKey(1);
        if(sysAcl == null){
            sysAcl = new SysAcl();
            sysAcl.setId(1);
            sysAcl.setName("myTest");
        }
        log.info(JsonMapper.obj2String(sysAcl));
        return JsonData.success("testGetBeanAndJsonMapper");
    }
}
